//
//  WBHomeController.swift
//  swiftweibo
//
//  Created by ffxk007 on 2017/6/1.
//  Copyright © 2017年 ffxk007. All rights reserved.
//

import UIKit

private let cellId = "cellId"


class WBHomeController: WBBaseViewController {

    
    /// 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func loadData() {
        
        //用网络工具加载微博数据
        print("加载数据结束 \(self.listViewModel.statusList.last?.text ?? "nil")")
       listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,hasMorePullup) in
//        print("加载数据结束 \(self.listViewModel.statusList.last?.text ?? "nil")")
        
        //结束刷新控件
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            print("刷新表格")
            if hasMorePullup {
                self.tableView?.reloadData()
            }
        
        }
        WBNetworkManger.shared.statusList { (list, isSuccess) in
//            print(list as Any)
        }
        
}
    
    @objc fileprivate func showFriend() {

        let vc = WBDemoViewController()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension WBHomeController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    // Declaration is only valid at file scope
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}

extension WBHomeController{
    
    
    override func setupTableview(){
        super.setupTableview()
        //设置导航栏按钮
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriend))
//        let btn:UIButton = UIButton()
//        btn.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
//        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
//        btn.setTitle("好友", for: UIControlState.normal)
//        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
//        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
//        btn.addTarget(self, action: #selector(showFriend), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriend))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        setupNavTitle()
    }
    
    /// 设置导航栏标题
    fileprivate func setupNavTitle() {
        let title = WBNetworkManger.shared.userAccount.screen_name
        let button = WBTitleButton(title: title)
        
        //设置主页上面的位置以及图标颜色
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        navItem.titleView = button
    }
    
    @objc func clickButton(btn:UIButton){
       
        btn.isSelected = !btn.isSelected
    }
}
